#pragma once
#include "RBTree.h"
namespace MySet
{
	template<class K>
	class set
	{
		struct SetKeyOfT {
			const K& operator()(const K& key)
			{
				return key;
			}
		};
	public:
		typedef typename RBTree<K, const K, SetKeyOfT>::Iterator iterator;
		typedef typename RBTree<K, const K, SetKeyOfT>::ConstIterator const_iterator;
		set() = default;
		set(initializer_list<K> il)
		{
			for (const auto& e : il)
			{
				_rbtree.Insert(e);
			}
		}
		template<class InputIterator>
		set(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_rbtree.Insert(*first);
				++first;
			}
		}
		set(const set& x)
		{
			//_rbtree = x._rbtree;
			for (const auto& e : x)
			{
				_rbtree.Insert(e);
			}
		}
		set& operator=(const set& x)
		{
			_rbtree = x._rbtree;
			return *this;
		}
		iterator begin()
		{
			return _rbtree.Begin();
		}
		iterator end()
		{
			return _rbtree.End();
		}
		const_iterator begin() const
		{
			return _rbtree.Begin();
		}
		const_iterator end() const
		{
			return _rbtree.End();
		}
		pair<iterator, bool> insert(const K& key)
		{
			return _rbtree.Insert(key);
		}
		iterator find(const K& key)
		{
			return _rbtree.Find(key);
		}
		void erase(const K& key)
		{
			_rbtree.Erase(key);
		}
	private:
		RBTree<K, const K, SetKeyOfT> _rbtree;
	};
	/*void test_set()
	{
		set<int> st;
		st.insert(1);
		st.insert(2);
		st.insert(3);
		st.insert(4);
		set<int>::iterator it = st.begin();
		while (it != st.end())
		{
			cout << *it << " ";
			++it;
		}
	}*/
	void Print(const set<int>& s)
	{
		set<int>::const_iterator it = s.end();
		while (it != s.begin())
		{
			--it;
			// 不支持修改
			//*it += 2;
			cout << *it << " ";
		}
		cout << endl;
	}
	void test_set()
	{
		set<int> s;
		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
		for (auto e : a)
		{
			s.insert(e);
		}
		for (auto e : s)
		{
			cout << e << " ";
		}
		cout << endl;
		Print(s);

		set<int> st = s;
		st.insert(100);
		Print(st);
		Print(s);

		st.erase(16);
		st.erase(100);
		for (auto e : st)
		{
			cout << e << " ";
		}
	}
}